package pe.candyncoffee.mytrace;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.Serializable;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

public class DrawablePack implements Parcelable{
String TAG = "MYTRACE";
	Drawable drawable;
	int width;
	int height;
	
	public DrawablePack(Drawable drawable) {
		if (drawable != null){
			this.drawable = drawable;
			width = drawable.getBounds().width();
			height = drawable.getBounds().height();
		}
	}
	public DrawablePack(Parcel src){

		byte array[] = new byte[src.dataSize()];
		//Log.d(TAG, "After : " + Integer.toString(array.length) + ", " + Integer.toString(src.dataSize()));
		
		
		if (src != null){
			array = src.createByteArray();
			//src.readByteArray(array);
			InputStream is = new BufferedInputStream(new ByteArrayInputStream(array));
			drawable = Drawable.createFromStream(is, "src");
			width = drawable.getBounds().width();
			height = drawable.getBounds().height();
		}
	}
	
	public Drawable getDrawable(){
		return drawable;
	}
	
	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
		byte array[] = out.toByteArray();
		
		dest.writeByteArray(array);
	}
	
	public static final Parcelable.Creator<DrawablePack> CREATOR = new Creator<DrawablePack>() {
		@Override
		public DrawablePack createFromParcel(Parcel source) {
			return new DrawablePack(source);
		}
		@Override
		public DrawablePack[] newArray(int size) {
			return new DrawablePack[size];
		}
	};

}
